import AES from 'crypto-js/aes'
import UTF8 from 'crypto-js/enc-utf8'
import LATIN1 from 'crypto-js/enc-latin1'
import CFB from 'crypto-js/mode-cfb'
import ECB from 'crypto-js/mode-ecb'
import NoPadding from 'crypto-js/pad-nopadding'
import Pkcs7 from 'crypto-js/pad-pkcs7'

/**
 * @text 要加密的内容
 * @secret String  服务器随机返回的关键字
 *  */
export const aesEncrypt = (text, secret = 'XwKsGlMcdPMEhR1B') => {
  const key = UTF8.parse(secret)
  const data = UTF8.parse(text)
  const encrypted = AES.encrypt(data, key, { mode: ECB, padding: Pkcs7 })
  return encrypted.toString()
}

export const aesEncryptBiz = (text, secret) => {
  const key = LATIN1.parse(secret)
  const encrypted = AES.encrypt(text, key, { iv: key, mode: CFB, padding: NoPadding })
  return encrypted.toString()
}
export const base64Encode = text => {
  return window.btoa(text)
}

export const encrypt = (text, secret) => {
  const encrypted = AES.encrypt(text, secret)
  return encrypted.toString()
}
export const decrypt = (encrypted, secret) => {
  const decrypted = AES.decrypt(encrypted, secret)
  return decrypted.toString(UTF8)
}
